<?php
namespace app\admin\controller;

use app\admin\model\Setting as SettingModel;
use think\facade\View;
use think\facade\Cache;

/**
 * 开放平台对接
 */
class Open extends Controller
{
    /**
     * 微信开放平台第三方平台设置
     */
    public function wxopen()
    {
        return $this->updateEvent('wxopen');
    }
	
	/**
     * 微信开放平台网站应用设置
     */
    public function wxweb()
    {
        return $this->updateEvent('wxweb');
    }

    /**
     * 更新设置事件
     */
    private function updateEvent(string $key)
    {
        if (!$this->request->isAjax()) {
            $model = SettingModel::getItem($key);
            if($key == 'wxopen'){
                $status = ['失败','正常'];
                if(Cache::get('component_access_token')){
                    $value = 1;
                }else{
                    $value = 0;
                }
                $model['status'] = ['text' => $status[$value], 'value' => $value];
            }
            return View::fetch($key, compact('model'));
        }
        $model = new SettingModel;
        if ($model->edit($key,$this->postData('data'))) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError('更新失败');
    }

}
